﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using FangChanCrm.Services.Interface;
using FangChanCrmEF;
using FangChanCrmModel;
using Maticsoft.Model;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.StaticFiles;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;

namespace FangChanCrm.Controllers
{
    public class RenYuanController : Controller
    {
        private readonly It_RenYuanBiaoService t_RenYuanBiaoService;
        private readonly It_BuMenBiaoService t_BuMenBiaoService;
        private readonly It_GangWeiBiaoService t_GangWeiBiaoService;
        private readonly It_GongSiBiaoService t_GongSiBiaoService;
        private readonly It_ZhanDuiBiaoService t_ZhanDuiBiaoService;
        private readonly It_ZhanQuBiaoService t_ZhanQuBiaoService;
        private readonly It_XiaoShouBiaoService t_XiaoShouBiaoService;
        private readonly It_XiaoShouXiangXiBiaoService t_XiaoShouXiangXiBiaoService;
        private readonly It_KeHuBiaoService t_KeHuBiaoService;
        private readonly It_QuanXianPeiZhiBiaoService t_QuanXianPeiZhiBiaoService;
        private readonly It_RenYuanQuanXianService t_RenYuanQuanXianService;
        private readonly IWebHostEnvironment WebHostEnvironment;

        public RenYuanController(It_RenYuanBiaoService renYuanBiaoService, It_BuMenBiaoService buMenBiaoService, It_GangWeiBiaoService gangWeiBiaoService, It_GongSiBiaoService gongSiBiaoService, It_ZhanDuiBiaoService zhanDuiBiaoService, It_ZhanQuBiaoService zhanQuBiaoService, It_XiaoShouBiaoService xiaoShouBiaoService, It_XiaoShouXiangXiBiaoService xiaoShouXiangXiBiaoService, It_KeHuBiaoService keHuBiaoService, IWebHostEnvironment webHostEnvironment, IConfiguration configuration, It_QuanXianPeiZhiBiaoService QuanXianPeiZhiBiaoService, It_RenYuanQuanXianService RenYuanQuanXianService)
        {
            t_RenYuanBiaoService = renYuanBiaoService;
            t_BuMenBiaoService = buMenBiaoService;
            t_GangWeiBiaoService = gangWeiBiaoService;
            t_GongSiBiaoService = gongSiBiaoService;
            t_ZhanDuiBiaoService = zhanDuiBiaoService;
            t_ZhanQuBiaoService = zhanQuBiaoService;
            t_XiaoShouBiaoService = xiaoShouBiaoService;
            t_XiaoShouXiangXiBiaoService = xiaoShouXiangXiBiaoService;
            t_KeHuBiaoService = keHuBiaoService;
            WebHostEnvironment = webHostEnvironment;
            Configuration = configuration;
            t_QuanXianPeiZhiBiaoService = QuanXianPeiZhiBiaoService;
            t_RenYuanQuanXianService = RenYuanQuanXianService;
        }

        public IConfiguration Configuration { get; }
        public IActionResult List()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult CreateAndModify()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult RenYuanXiangXi()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }
        public IActionResult YiLiZhiList()
        {
            if (HttpContext.Session.GetString("loginid") == null)
            {
                HttpContext.Request.Cookies.TryGetValue("账户", out string value);
                if (value == null || value == "")
                {
                    return View("../Login/Login");
                }
                else
                {
                    return View("../Login/LoginChongXin");
                }
            }
            else
            {
                return View();
            }
        }

        public IActionResult BindWeiXin()
        {
            return View();
        }

        public IActionResult PhoneMiMaXueGai()
        {
            return View();
        }

        [HttpPost]
        public string InitializationDataTable()
        {
            t_RenYuanBiao t_renYuanBiao = new t_RenYuanBiao();
            t_renYuanBiao.BeiZhu9 = Convert.ToInt32(Request.Form["selGongSi"]).ToString();
            t_renYuanBiao.BeiZhu10 = Convert.ToInt32(Request.Form["selBuMen"]).ToString();
            t_renYuanBiao.GangWeiId = Convert.ToInt32(Request.Form["selGangWei"]);
            t_renYuanBiao.ZhanQuId = Convert.ToInt32(Request.Form["selZhanQu"]);
            t_renYuanBiao.ZhanDuiId = Convert.ToInt32(Request.Form["selZhanDui"]);
            t_renYuanBiao.XingMing = Request.Form["xingming"];
            t_renYuanBiao.ZhuangTai = Request.Form["ZhuangTai"];
            return t_RenYuanBiaoService.GetListWithFenYe1(t_renYuanBiao, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        public string FuZhi()
        {
            bool b = false;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["RebYuanId"]));
                RenYuan.BeiZhu2 = HttpContext.Session.GetString("loginid");
                RenYuan.ZhuangTai = "在职";
                RenYuan.LiZhiShiJian = DateTime.Now.ToString();
                _context.SaveChanges();
            }
            if (b)
            {
                return "success";
            }
            else
            {
                return "error";
            }
        }
        [HttpPost]
        public string Create()
        {
            List<t_RenYuanBiao> panduan = t_RenYuanBiaoService.GetList().Where(x => x.ZhangHao == Request.Form["ZhangHao"]).ToList();
            if (panduan.Count() >= 1)
            {
                return "error2";
            }
            else
            {
                t_RenYuanBiao t_RenYuanBiao = new t_RenYuanBiao();
                t_RenYuanBiao.XingMing = Request.Form["XingMing"];
                t_RenYuanBiao.ZhangHao = Request.Form["ZhangHao"];
                t_RenYuanBiao.MiMa = Request.Form["MiMa"];
                t_RenYuanBiao.NianLing = Request.Form["NianLing"];
                t_RenYuanBiao.XingBie = Request.Form["XingBie"];
                t_RenYuanBiao.LianXiFangShi = Request.Form["LianXiFangShi"];
                if (Request.Form["GangWeiId"] == "" || Request.Form["GangWeiId"] == "0")
                {
                    t_RenYuanBiao.GangWeiId = 1;
                }
                else
                {
                    t_RenYuanBiao.GangWeiId = Convert.ToInt32(Request.Form["GangWeiId"]);

                }
                if (Request.Form["ZhanDuiId"] == "")
                {
                    t_RenYuanBiao.ZhanDuiId = 1;
                }
                else
                {
                    t_RenYuanBiao.ZhanDuiId = Convert.ToInt32(Request.Form["ZhanDuiId"]);
                }
                if (Request.Form["ZhanQuId"] == "")
                {
                    t_RenYuanBiao.ZhanDuiId = 1;
                }
                else
                {
                    t_RenYuanBiao.ZhanQuId = Convert.ToInt32(Request.Form["ZhanQuId"]);
                }
                t_RenYuanBiao.ZhuangTai = Request.Form["ZhuangTai"];
                t_RenYuanBiao.XingJi = Request.Form["XingJi"];
                t_RenYuanBiao.HunYinZhuangKuang = Request.Form["HunYinZhuangKuang"];
                t_RenYuanBiao.WeiXinHao = Request.Form["WeiXinHao"];
                t_RenYuanBiao.YouXiang = Request.Form["YouXiang"];
                t_RenYuanBiao.ShenFenZhengHao = Request.Form["ShenFenZhengHao"];
                t_RenYuanBiao.JiaTingDiZhi = Request.Form["JiaTingDiZhi"];
                t_RenYuanBiao.XueLi = Request.Form["XueLi"];
                t_RenYuanBiao.RuZhiShiJian = Request.Form["RuZhiShiJian"];
                t_RenYuanBiao.LiZhiShiJian = Request.Form["LiZhiShiJian"];
                if (t_RenYuanBiao.GangWeiId == 0 || t_RenYuanBiao.GangWeiId == null)
                {
                    t_RenYuanBiao.GangWeiId = 1;
                }
                int Id = (int)t_RenYuanBiaoService.Add(t_RenYuanBiao).Id;

                if (Id > 0)
                {
                    #region 使用岗位判断权限配置
                    t_GangWeiBiao t_GangWeiBiao = new t_GangWeiBiao();
                    if (t_RenYuanBiao.GangWeiId != 1)
                    {
                        t_GangWeiBiao = t_GangWeiBiaoService.GetById(t_RenYuanBiao.GangWeiId);
                    }
                    if (t_GangWeiBiao != null)
                    {
                        List<t_QuanXianPeiZhiBiao> list = t_QuanXianPeiZhiBiaoService.GetList();
                        for (int i = 0; i < list.Count; i++)
                        {
                            if (t_GangWeiBiao.GangWeiMingCheng == list[i].PeiZhiMingCheng)
                            {
                                t_RenYuanQuanXian t_RenYuanQuanXian = new t_RenYuanQuanXian();
                                t_RenYuanQuanXian.RenYuanId = Id;
                                t_RenYuanQuanXian.QuanXianId = list[i].QuanXianId;
                                t_RenYuanQuanXianService.Add(t_RenYuanQuanXian);
                                break;
                            }
                        }
                    }
                    #endregion
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
        }
        [HttpPost]
        public string Modify()
        {
            List<t_RenYuanBiao> panduan = t_RenYuanBiaoService.GetList().Where(x => x.ZhangHao == Request.Form["ZhangHao"] && x.Id != Convert.ToInt32(Request.Form["Id"])).ToList();
            if (panduan.Count() >= 1)
            {
                return "error2";
            }
            else
            {
                bool b = false;
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                    RenYuan.XingMing = Request.Form["XingMing"];
                    RenYuan.ZhangHao = Request.Form["ZhangHao"];
                    RenYuan.MiMa = Request.Form["MiMa"];
                    RenYuan.NianLing = Request.Form["NianLing"];
                    RenYuan.XingBie = Request.Form["XingBie"];
                    RenYuan.LianXiFangShi = Request.Form["LianXiFangShi"];
                    RenYuan.GangWeiId = Convert.ToInt32(Request.Form["GangWeiId"]);
                    RenYuan.ZhuangTai = Request.Form["ZhuangTai"];
                    RenYuan.XingJi = Request.Form["XingJi"];
                    RenYuan.ZhanDuiId = Convert.ToInt32(Request.Form["ZhanDuiId"]);
                    RenYuan.ZhanQuId = Convert.ToInt32(Request.Form["ZhanQuId"]);
                    RenYuan.HunYinZhuangKuang = Request.Form["HunYinZhuangKuang"];
                    RenYuan.WeiXinHao = Request.Form["WeiXinHao"];
                    RenYuan.YouXiang = Request.Form["YouXiang"];
                    RenYuan.ShenFenZhengHao = Request.Form["ShenFenZhengHao"];
                    RenYuan.JiaTingDiZhi = Request.Form["JiaTingDiZhi"];
                    RenYuan.XueLi = Request.Form["XueLi"];
                    RenYuan.RuZhiShiJian = Request.Form["RuZhiShiJian"];
                    RenYuan.LiZhiShiJian = Request.Form["LiZhiShiJian"];
                    b = _context.SaveChanges() > 0;
                }
                if (b)
                {
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
        }


        [HttpPost]
        public string Modify2()
        {
            t_RenYuanBiao t_RenYuanBiao = t_RenYuanBiaoService.GetById(Convert.ToInt32(Request.Form["Id"]));
            if (t_RenYuanBiao.BeiZhu1 == null || t_RenYuanBiao.BeiZhu1 == "")
            {
                return "";
            }
            else
            {
                string ReturnStr = "";
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["Id"]));
                    RenYuan.BeiZhu1 = "";
                    if (_context.SaveChanges() > 0)
                    {
                        ReturnStr = "success";
                    }
                    else
                    {
                        ReturnStr = "error";
                    }
                }
                return ReturnStr;
            }
        }



        [HttpPost]
        public string GetSelBuMen()
        {
            return JsonConvert.SerializeObject(t_BuMenBiaoService.GetList().Where(x => x.ZhuangTai == "启用" && x.GongSiId == Convert.ToInt32(Request.Form["GongSiId"])), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string ReturnIsZhanQuBuMen()
        {
            if (t_BuMenBiaoService.GetById(Convert.ToInt32(Request.Form["BuMenId"])).ZhanQuZhuangTai == "普通部门")
            {
                return "no";
            }
            else
            {
                return "yes";
            }
        }
        [HttpPost]
        public string GetSelGangWei()
        {
            return JsonConvert.SerializeObject(t_GangWeiBiaoService.GetList().Where(x => x.BuMenId == Convert.ToInt32(Request.Form["BuMenId"])), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string GetSelGongSi()
        {
            return JsonConvert.SerializeObject(t_GongSiBiaoService.GetList(), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string GetSelZhanDui()
        {
            return JsonConvert.SerializeObject(t_ZhanDuiBiaoService.GetList().Where(x => x.ZhuangTai == "启用" && x.ZhanQuId == Convert.ToInt32(Request.Form["ZhanQuId"])), new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string GetSelZhanQu()
        {
            List<t_ZhanQuBiao> list = null;
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var context = new MyContext(optionsBuilder.Options))
            {
                list = context.t_ZhanQuBiao.Where(x => x.ZhuangTai == "启用").ToList();
            }
            return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string ReturnRenYuanJson()
        {
            var test = t_RenYuanBiaoService.GetById(Convert.ToInt32(Request.Form["Id"]));
            test.t_GangWeiBiao.t_BuMenBiao = t_BuMenBiaoService.GetById((int)test.t_GangWeiBiao.BuMenId);
            test.t_GangWeiBiao.t_BuMenBiao.t_GongSiBiao = t_GongSiBiaoService.GetById((int)test.t_GangWeiBiao.t_BuMenBiao.GongSiId);
            test.t_ZhanDuiBiao.t_ZhanQuBiao = t_ZhanQuBiaoService.GetId((int)test.t_ZhanDuiBiao.ZhanQuId);
            return JsonConvert.SerializeObject(new List<t_RenYuanBiao> { test }, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string BindWeiXinUserId()
        {
            var sadfasd = Request.Form["ZhangHao"].ToString();
            var MiMa = Request.Form["MiMa"].ToString();
            List<t_RenYuanBiao> list = t_RenYuanBiaoService.GetList().Where(c => c.ZhangHao == Request.Form["ZhangHao"].ToString() && c.MiMa == Request.Form["MiMa"].ToString()).ToList();//账号密码查找
            if (list.Count > 0)
            {
                for (int i = 0; i < list.Count; i++)
                {
                    list[i].t_GangWeiBiao = null;
                    list[i].t_ZhanDuiBiao = null;
                }
            }
            if (list.Count != 0)
            {
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                bool buer = false;
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var people = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == list[0].Id);
                    people.BeiZhu1 = Request.Form["WeiXinUserId"];
                    buer = _context.SaveChanges() > 0;
                }
                if (buer)
                {
                    HttpContext.Session.SetString("loginid", list[0].Id.ToString());
                    HttpContext.Session.SetString("UserId", list[0].Id.ToString());
                    return "success";
                }
                else
                {
                    return "error";
                }
            }
            else
            {
                List<t_RenYuanBiao> list2 = t_RenYuanBiaoService.GetList().Where(c => c.ZhangHao == Request.Form["ZhangHao"]).ToList();//账号查找
                if (list2.Count != 0)
                {
                    return "mimaerror";
                }
                else
                {
                    return "zhanghaoerror";
                }
            }
        }
        [HttpPost]
        public string ReturnZhuanHuaLv()
        {
            var StartTime = Convert.ToDateTime(Request.Form["StartTime"]);
            var EndTime = Convert.ToDateTime(Request.Form["EndTime"]);
            var listxiaoshou = t_XiaoShouBiaoService.GetList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > StartTime && Convert.ToDateTime(x.ShengChengShiJian) < EndTime).ToList();
            var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
            var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员" || x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
            List<RetXingMingAndZhuanhualv> list = new List<RetXingMingAndZhuanhualv>();
            foreach (var item in listren)
            {
                List<t_XiaoShouBiao> tlistxiaoshou = listxiaoshou.Where(x => x.YeWuYuanId == item.Id || x.ZhiYeGuWenId == item.Id).ToList();
                if (tlistxiaoshou.Count == 0)
                {
                    list.Add(new RetXingMingAndZhuanhualv { Name = item.XingMing, ZhuanHua = "0%", Id = (int)item.Id, ZongShu = 0, ChengJiaoShu = 0, GangWei = t_GangWeiBiaoService.GetById(Convert.ToInt32(item.GangWeiId)).GangWeiMingCheng });
                }
                else
                {
                    int count = 0;
                    foreach (var item0 in tlistxiaoshou)
                    {
                        List<t_XiaoShouXiangXiBiao> txiaoshouxiangxi = listxiaoshouxiangxi.Where(x => x.XiaoShouBiaoId == item0.Id && x.XiaoShouDongZuo == "已认购").ToList();
                        if (txiaoshouxiangxi.Count != 0)
                        {
                            count++;
                        }
                    }
                    if (tlistxiaoshou.Count == 0)
                    {
                        list.Add(new RetXingMingAndZhuanhualv { Name = item.XingMing, ZhuanHua = "0%", Id = (int)item.Id, ZongShu = 0, ChengJiaoShu = 0, GangWei = t_GangWeiBiaoService.GetById(Convert.ToInt32(item.GangWeiId)).GangWeiMingCheng });
                    }
                    else
                    {
                        list.Add(new RetXingMingAndZhuanhualv { Name = item.XingMing, ZhuanHua = (Convert.ToDouble(count) / Convert.ToDouble(tlistxiaoshou.Count)).ToString("0%"), Id = (int)item.Id, ZongShu = tlistxiaoshou.Count, ChengJiaoShu = count, GangWei = t_GangWeiBiaoService.GetById(Convert.ToInt32(item.GangWeiId)).GangWeiMingCheng });
                    }
                }
            }
            if (listren.Where(x => x.Id == Convert.ToInt32(/*HttpContext.Session.GetString("loginid")*/3)).ToList().Count > 0)
            {
                list = list.Where(x => x.Id == Convert.ToInt32(/*HttpContext.Session.GetString("loginid")*/3)).ToList();
            }
            return JsonConvert.SerializeObject(list, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        [HttpPost]
        public string GetRenYuanXiangXi()
        {
            string shuju = Request.Form["RenYuanId"].ToString();
            int id = Convert.ToInt32(shuju);
            t_RenYuanBiao t_RenYuanBiao = t_RenYuanBiaoService.GetById(id);

            int gangweiid = Convert.ToInt32(t_RenYuanBiao.GangWeiId);
            t_GangWeiBiao gw = t_GangWeiBiaoService.GetById(gangweiid);

            t_BuMenBiao bm = t_BuMenBiaoService.GetById(Convert.ToInt32(gw.BuMenId));

            t_GongSiBiao gs = t_GongSiBiaoService.GetById(Convert.ToInt32(bm.GongSiId));

            string zhanqumingcheng = "空";
            string zhanduimingcheng = "空";
            string shoushubumenzongjian = "空";
            string ZhanQuFuZeRenshuju = "空";
            string lizhishijianshuju = "空";

            if (t_RenYuanBiao.t_ZhanQuBiao != null)
            {
                zhanqumingcheng = t_RenYuanBiao.t_ZhanQuBiao.ZhanQuMingCheng;
                ZhanQuFuZeRenshuju = t_RenYuanBiao.t_ZhanQuBiao.ZhanQuFuZeRen.ToString();
            }

            if (t_RenYuanBiao.t_ZhanDuiBiao != null)
            {
                zhanduimingcheng = t_RenYuanBiao.t_ZhanDuiBiao.ZhanDuiMingCheng;
            }

            if (t_RenYuanBiao.LiZhiShiJian != null)
            {
                lizhishijianshuju = t_RenYuanBiao.LiZhiShiJian;
            }

            string XingMing = "空";
            string XingBie = "空";
            string NianLing = "空";
            string LianXiFangShi = "空";
            string RuZhiShiJian = "空";

            if (t_RenYuanBiao.XingMing != null && t_RenYuanBiao.XingMing != "")
            {
                XingMing = t_RenYuanBiao.XingMing;
            }
            if (t_RenYuanBiao.XingBie != null && t_RenYuanBiao.XingBie != "")
            {
                XingBie = t_RenYuanBiao.XingBie;
            }
            if (t_RenYuanBiao.NianLing != null && t_RenYuanBiao.NianLing != "")
            {
                NianLing = t_RenYuanBiao.NianLing;
            }
            if (t_RenYuanBiao.LianXiFangShi != null && t_RenYuanBiao.LianXiFangShi != "")
            {
                LianXiFangShi = t_RenYuanBiao.LianXiFangShi;
            }
            if (t_RenYuanBiao.RuZhiShiJian != null && t_RenYuanBiao.RuZhiShiJian != "")
            {
                RuZhiShiJian = t_RenYuanBiao.RuZhiShiJian;
            }

            XingMing = XingMing.Trim();
            XingBie = XingBie.Trim();
            NianLing = NianLing.Trim();
            LianXiFangShi = LianXiFangShi.Trim();
            RuZhiShiJian = RuZhiShiJian.Trim();
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            Json.Append("{\"renyuanmingcheng\":\"" + XingMing + "\",\"xingbie\":\"" + XingBie + "\",\"nianling\":\"" + NianLing + "\",\"lianxifangshi\":\"" + LianXiFangShi + "\",\"gongsimingcheng\":\"" + gs.GongSiMingCheng + "\",\"bumenmingcheng\":\"" + bm.BuMenMingCheng + "\",\"gangweimingcheng\":\"" + t_RenYuanBiao.t_GangWeiBiao.GangWeiMingCheng + "\",\"zhanqumingcheng\":\"" + zhanqumingcheng + "\",\"zhanduimingcheng\":\"" + zhanduimingcheng + "\",\"shoushuzhanquzhong\":\"" + ZhanQuFuZeRenshuju + "\",\"shoushubumenzongjian\":\"" + shoushubumenzongjian + "\",\"ruzhishijian\":\"" + RuZhiShiJian + "\",\"lizhishijian\":\"" + lizhishijianshuju + "\"}");
            Json.Append("]");
            return Json.ToString().Trim();
        }
        public class RetXingMingAndZhuanhualv
        {
            public int Id { get; set; }
            public string Name { get; set; }
            public string ZhuanHua { get; set; }
            public int ZongShu { get; set; }
            public int ChengJiaoShu { get; set; }
            public string GangWei { get; set; }
        }
        public string PhoneNumberList()
        {
            List<t_RenYuanBiao> listren = new List<t_RenYuanBiao>();
            var list = t_ZhanDuiBiaoService.GetList().Where(x => x.ZhanQuId == 19).ToList();
            foreach (var item in list)
            {
                if (item.t_RenYuanBiaos != null)
                {
                    listren.AddRange(item.t_RenYuanBiaos);
                }
            }
            listren = listren.DistinctBy(x => x.Id).ToList();
            return null;
        }
        public string KaiChu()
        {
            string ReturnStr = "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["RebYuanId"]));
                RenYuan.ZhuangTai = "离职";
                RenYuan.BeiZhu2 = HttpContext.Session.GetString("loginid");
                RenYuan.LiZhiShiJian = DateTime.Now.ToString();
                if (_context.SaveChanges() > 0)
                {
                    ReturnStr = "success";
                }
                else
                {
                    ReturnStr = "error";
                }
            }
            return ReturnStr;
        }

        public string GetKehuListshuju()
        {
            string kaishishijian = Request.Form["kaishishijian"].ToString();
            string jieshushijian = Request.Form["jieshushijian"].ToString();
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            return t_RenYuanBiaoService.GetKeHuShuJu(kaishishijian, jieshushijian, RenYuanId, Convert.ToInt32(Request.Form["start"]), Convert.ToInt32(Request.Form["limit"]), Request.Form["dir"], Convert.ToInt32(Request.Form["column"]));
        }
        public string GetZongXiaoShouShuJu()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            List<t_XiaoShouXiangXiBiao> xx = t_RenYuanBiaoService.ZongGetXiaoshouXinXiShuJu(RenYuanId, "", "");
            string kehuliang = xx.Count.ToString();
            List<t_XiaoShouXiangXiBiao> xxx = t_RenYuanBiaoService.ZongGetXiaoshouXinXiShuJu(RenYuanId, "带看", "");
            string daikanshu = xxx.Count.ToString();

            var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购").ToList();
            List<t_XiaoShouXiangXiBiao> chengjiaoshu = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == RenYuanId || x.t_XiaoShouBiao.ZhiYeGuWenId == RenYuanId).ToList();

            double jiner = 0;
            for (int i = 0; i < chengjiaoshu.Count; i++)
            {
                if (chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE != null && chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE != "")
                {
                    jiner += Convert.ToDouble(chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE);
                }
            }

            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            Json.Append("{\"kehuliang\":\"" + kehuliang + "\",\"daikanshu\":\"" + daikanshu + "\",\"chengjiaoshu\":\"" + chengjiaoshu.Count.ToString() + "\",\"jiner\":\"" + jiner + "\"}");
            Json.Append("]");
            return Json.ToString();
        }
        public string GetXiaoShouShuJu()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            string kaishishijian = Request.Form["kaishishijian"].ToString();
            string jieshushijian = Request.Form["jieshushijian"].ToString();
            List<t_XiaoShouXiangXiBiao> xx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(RenYuanId, kaishishijian, jieshushijian, "", "");
            string kehuliang = xx.Count.ToString();
            List<t_XiaoShouXiangXiBiao> xxx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(RenYuanId, kaishishijian, jieshushijian, "带看", "");
            string daikanshu = xxx.Count.ToString();
            List<t_XiaoShouXiangXiBiao> chengjiaoshu = new List<t_XiaoShouXiangXiBiao>();
            if (kaishishijian != null && kaishishijian != "" && jieshushijian != null && jieshushijian != "")
            {
                kaishishijian += " 0:00:00";
                jieshushijian += " 23:59:59";
                DateTime kaishi = Convert.ToDateTime(kaishishijian);
                DateTime jiezhi = Convert.ToDateTime(jieshushijian);
                var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) >= kaishi && Convert.ToDateTime(x.CaoZuoShiJian) <= jiezhi).ToList();
                chengjiaoshu = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == RenYuanId || x.t_XiaoShouBiao.ZhiYeGuWenId == RenYuanId).ToList();
            }
            else
            {
                int year = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
                int month = Convert.ToInt32(DateTime.Now.ToString("MM"));
                int days = DateTime.DaysInMonth(year, month);
                DateTime t1 = new DateTime(year, month, days);
                DateTime t2 = new DateTime(year, month, 1);
                String jiezhishijian1 = t1.ToString("yyyy-MM-dd");
                jiezhishijian1 += " 23:59:59";
                DateTime kaishi = t2;
                DateTime jiezhi = Convert.ToDateTime(jiezhishijian1);
                var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) >= kaishi && Convert.ToDateTime(x.CaoZuoShiJian) <= jiezhi).ToList();
                chengjiaoshu = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == RenYuanId || x.t_XiaoShouBiao.ZhiYeGuWenId == RenYuanId).ToList();
            }

            double jiner = 0;
            for (int i = 0; i < chengjiaoshu.Count; i++)
            {
                if (chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE != null && chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE != "")
                {
                    jiner += Convert.ToDouble(chengjiaoshu[i].t_XiaoShouBiao.ChengJiaoJinE);
                }
            }

            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            Json.Append("{\"kehuliang\":\"" + kehuliang + "\",\"daikanshu\":\"" + daikanshu + "\",\"chengjiaoshu\":\"" + chengjiaoshu.Count.ToString() + "\",\"jiner\":\"" + jiner + "\"}");
            Json.Append("]");
            return Json.ToString();
        }

        public string GetXiaoShouShuJuJingLi()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            string kaishishijian = Request.Form["kaishishijian"].ToString();
            string jieshushijian = Request.Form["jieshushijian"].ToString();

            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(RenYuanId);
            List<t_RenYuanBiao> rylist = t_RenYuanBiaoService.GetZhanDuiRenYuanList(Convert.ToInt32(ry.ZhanDuiId));

            int kehuliang = 0;
            int daikanshu = 0;
            double jiner = 0;
            int xiaoshoshu = 0;
            List<t_XiaoShouXiangXiBiao> chengjiaoshu = new List<t_XiaoShouXiangXiBiao>();
            DateTime kaishi;
            DateTime jiezhi;
            string ka = "";
            string js = "";
            if (kaishishijian != null && kaishishijian != "" && jieshushijian != null && jieshushijian != "")
            {
                ka = kaishishijian + " 0:00:00";
                js = jieshushijian + " 23:59:59";
                kaishi = Convert.ToDateTime(ka);
                jiezhi = Convert.ToDateTime(js);
            }
            else
            {
                int year = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
                int month = Convert.ToInt32(DateTime.Now.ToString("MM"));
                int days = DateTime.DaysInMonth(year, month);
                DateTime t1 = new DateTime(year, month, days);
                DateTime t2 = new DateTime(year, month, 1);
                String jiezhishijian1 = t1.ToString("yyyy-MM-dd");
                jiezhishijian1 += " 23:59:59";
                kaishi = t2;
                jiezhi = Convert.ToDateTime(jiezhishijian1);
            }


            for (int i = 0; i < rylist.Count; i++)
            {
                List<t_XiaoShouXiangXiBiao> xx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(Convert.ToInt32(rylist[i].Id), kaishishijian, jieshushijian, "", "");
                kehuliang = kehuliang + xx.Count;
                List<t_XiaoShouXiangXiBiao> xxx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(Convert.ToInt32(rylist[i].Id), kaishishijian, jieshushijian, "带看", "");
                daikanshu = daikanshu + xxx.Count;
                var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) >= kaishi && Convert.ToDateTime(x.CaoZuoShiJian) <= jiezhi).ToList();
                chengjiaoshu = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == Convert.ToInt32(rylist[i].Id) || x.t_XiaoShouBiao.ZhiYeGuWenId == Convert.ToInt32(rylist[i].Id)).ToList();
                xiaoshoshu += chengjiaoshu.Count;
                for (int k = 0; k < chengjiaoshu.Count; k++)
                {
                    if (chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE != null && chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE != "")
                    {
                        jiner += Convert.ToDouble(chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE);
                    }
                }
            }
            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            Json.Append("{\"kehuliang\":\"" + kehuliang + "\",\"daikanshu\":\"" + daikanshu + "\",\"chengjiaoshu\":\"" + xiaoshoshu + "\",\"jiner\":\"" + jiner + "\"}");
            Json.Append("]");
            return Json.ToString();
        }

        public string GetXiaoShouShuJuZongJian()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            string kaishishijian = Request.Form["kaishishijian"].ToString();
            string jieshushijian = Request.Form["jieshushijian"].ToString();

            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(RenYuanId);
            List<t_ZhanDuiBiao> zdlist = t_RenYuanBiaoService.GetZhanQuZhanDuiList(Convert.ToInt32(ry.ZhanQuId));

            int kehuliang = 0;
            int daikanshu = 0;
            double jiner = 0;
            int xiaoshoshu = 0;
            List<t_XiaoShouXiangXiBiao> chengjiaoshu = new List<t_XiaoShouXiangXiBiao>();
            DateTime kaishi;
            DateTime jiezhi;
            string ka = "";
            string js = "";
            if (kaishishijian != null && kaishishijian != "" && jieshushijian != null && jieshushijian != "")
            {
                ka = kaishishijian + " 0:00:00";
                js = jieshushijian + " 23:59:59";
                kaishi = Convert.ToDateTime(ka);
                jiezhi = Convert.ToDateTime(js);
            }
            else
            {
                int year = Convert.ToInt32(DateTime.Now.ToString("yyyy"));
                int month = Convert.ToInt32(DateTime.Now.ToString("MM"));
                int days = DateTime.DaysInMonth(year, month);
                DateTime t1 = new DateTime(year, month, days);
                DateTime t2 = new DateTime(year, month, 1);
                String jiezhishijian1 = t1.ToString("yyyy-MM-dd");
                jiezhishijian1 += " 23:59:59";
                kaishi = t2;
                jiezhi = Convert.ToDateTime(jiezhishijian1);
            }


            for (int j = 0; j < zdlist.Count; j++)
            {
                List<t_RenYuanBiao> rylist = t_RenYuanBiaoService.GetZhanDuiRenYuanList(Convert.ToInt32(zdlist[j].id));
                for (int i = 0; i < rylist.Count; i++)
                {
                    List<t_XiaoShouXiangXiBiao> xx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(Convert.ToInt32(rylist[i].Id), kaishishijian, jieshushijian, "", "");
                    kehuliang = kehuliang + xx.Count;
                    List<t_XiaoShouXiangXiBiao> xxx = t_RenYuanBiaoService.GetXiaoshouXinXiShuJu(Convert.ToInt32(rylist[i].Id), kaishishijian, jieshushijian, "带看", "");
                    daikanshu = daikanshu + xxx.Count;
                    var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) >= kaishi && Convert.ToDateTime(x.CaoZuoShiJian) <= jiezhi).ToList();
                    chengjiaoshu = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == Convert.ToInt32(rylist[i].Id) || x.t_XiaoShouBiao.ZhiYeGuWenId == Convert.ToInt32(rylist[i].Id)).ToList();
                    xiaoshoshu += chengjiaoshu.Count;
                    for (int k = 0; k < chengjiaoshu.Count; k++)
                    {
                        if (chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE != null && chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE != "")
                        {
                            jiner += Convert.ToDouble(chengjiaoshu[k].t_XiaoShouBiao.ChengJiaoJinE);
                        }
                    }
                }
            }


            StringBuilder Json = new StringBuilder();
            Json.Append("[");
            Json.Append("{\"kehuliang\":\"" + kehuliang + "\",\"daikanshu\":\"" + daikanshu + "\",\"chengjiaoshu\":\"" + xiaoshoshu + "\",\"jiner\":\"" + jiner + "\"}");
            Json.Append("]");
            return Json.ToString();
        }

        public string GetXingJi()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(RenYuanId);
            return ry.XingJi.ToString();
        }

        public string GetXingJiShiJian()
        {
            int RenYuanId = Convert.ToInt32(Request.Form["RenYuanId"].ToString());
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(RenYuanId);
            int month = Convert.ToInt32(DateTime.Now.ToString("MM"));

            if (ry.BeiZhu3 != null)
            {
                int monthf = Convert.ToInt32(Convert.ToDateTime(ry.BeiZhu3).ToString("MM"));
                if (month == monthf)
                {
                    return "false";
                }
                else
                {
                    return "true";
                }
            }
            else
            {
                return "true";
            }
        }

        public string AddXingJi()
        {
            string ReturnStr = "";
            var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
            optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
            using (var _context = new MyContext(optionsBuilder.Options))
            {
                var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["RenYuanId"]));
                RenYuan.BeiZhu3 = DateTime.Now.ToString();
                RenYuan.XingJi = Request.Form["xingji"].ToString();
                if (_context.SaveChanges() > 0)
                {
                    ReturnStr = "true";
                }
                else
                {
                    ReturnStr = "false";
                }
            }
            return ReturnStr;
        }

        public string HQZhangHao()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);
            return ry.ZhangHao.ToString();
        }

        public string XueGaiMiMa()
        {
            int id = Convert.ToInt32(HttpContext.Session.GetString("UserId"));
            string MiMa = Request.Form["MiMa"].ToString();
            string xinMiMa = Request.Form["xinMiMa"].ToString();
            t_RenYuanBiao ry = t_RenYuanBiaoService.GetById(id);
            if (ry.MiMa.Trim() == MiMa.Trim())
            {
                ry.MiMa = xinMiMa.ToString();
                bool pd = false;
                var optionsBuilder = new DbContextOptionsBuilder<MyContext>();
                optionsBuilder.UseSqlServer(Configuration.GetConnectionString("DefaultConnection"));
                using (var _context = new MyContext(optionsBuilder.Options))
                {
                    var RenYuan = _context.t_RenYuanBiao.FirstOrDefault(x => x.Id == Convert.ToInt32(Request.Form["RebYuanId"]));
                    RenYuan.MiMa = Request.Form["xinMiMa"].ToString();
                    pd = _context.SaveChanges() > 0;
                }
                if (pd)
                {
                    return "2";
                }
                else
                {
                    return "3";
                }
            }
            else
            {
                return "1";
            }
        }

        public List<t_KeHuBiao> allphonenumber()
        {
            var list = t_KeHuBiaoService.GetList().DistinctBy(x => x.ShouJiHaoMa).ToList();
            var sadf = Helper.ToDataTable(list);
            var filename = "wwwroot/" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.Hour + DateTime.Now.Minute + DateTime.Now.Second + ".xlsx".Trim();
            var sadf0 = Helper.DataTableToExcel(filename, sadf, "sheet1", false);
            return null;
        }
        #region 员工每月及全年业绩额及排名
        [HttpPost]
        public string PaiMing()
        {
            var StartTime = Convert.ToDateTime(Request.Form["StartTime"]);
            var EndTime = Convert.ToDateTime(Request.Form["EndTime"]);
            var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
            var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员" || x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
            for (int i = 0; i < listren.Count; i++)
            {
                listren[i].t_BeiWangLus = null;
                listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                listren[i].t_KeHuBiaos = null;
                listren[i].t_KeHuZiYuanChis = null;
                listren[i].t_RenYuanQuanXians = null;
                listren[i].t_XiaoShouBiao_QueRenRens = null;
                listren[i].t_XiaoShouBiao_YeWuYuans = null;
                listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                listren[i].t_XiaoXiTiXingBiaos = null;
                listren[i].t_YiJianFanKuiBiaos = null;
                listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                listren[i].BeiZhu10 = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList().Count.ToString();
            }
            if (Request.Form["ascordesc"] == "asc")
            {
                listren = listren.OrderBy(x => Convert.ToInt32(x.BeiZhu10)).ToList();
            }
            else
            {
                listren = listren.OrderByDescending(x => Convert.ToInt32(x.BeiZhu10)).ToList();
            }
            return JsonConvert.SerializeObject(listren, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 同岗位管理层每月及全年团队业绩额及排名
        [HttpPost]
        public string GangWeiPaiMing()
        {
            List<RetClass> retClasses = new List<RetClass>();
            var StartTime = Convert.ToDateTime(Request.Form["StartTime"]);
            var EndTime = Convert.ToDateTime(Request.Form["EndTime"]);
            var listxiaoshouxiangxi = t_XiaoShouXiangXiBiaoService.GetList().Where(x => x.XiaoShouDongZuo == "已认购" && Convert.ToDateTime(x.CaoZuoShiJian) > StartTime && Convert.ToDateTime(x.CaoZuoShiJian) < EndTime).ToList();
            var listren = t_RenYuanBiaoService.GetList().Where(x => x.t_GangWeiBiao.GangWeiMingCheng == "业务员" || x.t_GangWeiBiao.GangWeiMingCheng == "置业顾问").ToList();
            for (int i = 0; i < listren.Count; i++)
            {
                listren[i].t_BeiWangLus = null;
                listren[i].t_GangWeiBiao.t_RenYuanBiaos = null;
                listren[i].t_KeHuBiaos = null;
                listren[i].t_KeHuZiYuanChis = null;
                listren[i].t_RenYuanQuanXians = null;
                listren[i].t_XiaoShouBiao_QueRenRens = null;
                listren[i].t_XiaoShouBiao_YeWuYuans = null;
                listren[i].t_XiaoShouBiao_ZhiYeGuWens = null;
                listren[i].t_XiaoShouXiangXiBiao_CaoZuoRens = null;
                listren[i].t_XiaoShouXiangXiBiao_QueRenDaoFangRens = null;
                listren[i].t_XiaoXiTiXingBiaos = null;
                listren[i].t_YiJianFanKuiBiaos = null;
                listren[i].t_ZhanDuiBiao.t_RenYuanBiaos = null;
                listren[i].BeiZhu10 = listxiaoshouxiangxi.Where(x => x.t_XiaoShouBiao.YeWuYuanId == listren[i].Id || x.t_XiaoShouBiao.ZhiYeGuWenId == listren[i].Id).ToList().Count.ToString();
            }
            if (Request.Form["ZhanQuId"].ToString() == null)
            {
                var ZhanQuList = t_ZhanQuBiaoService.GetList();
                for (int i = 0; i < ZhanQuList.Count; i++)
                {
                    var listrenbybumen = listren.Where(x => x.t_ZhanDuiBiao.t_ZhanQuBiao.Id == ZhanQuList[i].Id);
                    RetClass retClass = new RetClass
                    {
                        Id = ZhanQuList[i].Id,
                        Name = ZhanQuList[i].ZhanQuMingCheng
                    };
                    foreach (var item in listrenbybumen)
                    {
                        retClass.DingDanShu += Convert.ToInt32(item.BeiZhu10);
                    }
                    retClasses.Add(retClass);
                }
            }
            else
            {
                var ZhanDuiList = t_ZhanDuiBiaoService.GetList();
                for (int i = 0; i < ZhanDuiList.Count; i++)
                {
                    var listrenbybumen = listren.Where(x => x.ZhanDuiId == ZhanDuiList[i].id);
                    RetClass retClass = new RetClass
                    {
                        Id = ZhanDuiList[i].id,
                        Name = ZhanDuiList[i].ZhanDuiMingCheng
                    };
                    foreach (var item in listrenbybumen)
                    {
                        retClass.DingDanShu += Convert.ToInt32(item.BeiZhu10);
                    }
                    retClasses.Add(retClass);
                }
            }
            if (Request.Form["ascordesc"] == "asc")
            {
                retClasses = retClasses.OrderBy(x => x.DingDanShu).ToList();
            }
            else
            {
                retClasses = retClasses.OrderByDescending(x => x.DingDanShu).ToList();
            }
            return JsonConvert.SerializeObject(retClasses, new JsonSerializerSettings { ReferenceLoopHandling = ReferenceLoopHandling.Ignore });
        }
        #endregion
        #region 公司本月及汇总新增客户数
        public string AddKeHuShu()
        {
            return t_XiaoShouBiaoService.GetList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > Convert.ToDateTime(Request.Form["StartTime"]) && Convert.ToDateTime(x.ShengChengShiJian) < Convert.ToDateTime(Request.Form["EndTime"])).ToList().Count.ToString();
        }
        #endregion
        #region 公司本月及汇总到访客户数
        public string AddDaoFangKeHuShu()
        {
            return t_XiaoShouBiaoService.GetList().Where(x => Convert.ToDateTime(x.ShengChengShiJian) > Convert.ToDateTime(Request.Form["StartTime"]) && Convert.ToDateTime(x.ShengChengShiJian) < Convert.ToDateTime(Request.Form["EndTime"]) && x.t_XiaoShouXiangXiBiaos.Where(z => z.XiaoShouDongZuo == "确认到访").ToList().Count > 0).ToList().Count.ToString();
        }
        #endregion
        #region 上传文件 下载文件
        [HttpGet]
        public IActionResult UpLoad(string file)
        {
            if (file != "" && file != null)
            {
                return View(DownLoad(file));
            }
            else
            {
                return View();
            }
        }
        [HttpPost]
        public string FileSave()
        {
            var date = Request;
            var files = Request.Form.Files;
            long size = files.Sum(f => f.Length);
            string webRootPath = WebHostEnvironment.WebRootPath;
            string contentRootPath = WebHostEnvironment.ContentRootPath;
            string retname = "";
            foreach (var formFile in files)
            {
                if (formFile.Length > 0)
                {
                    string fileExt = formFile.FileName;
                    long fileSize = formFile.Length;
                    string newFileName = fileExt;
                    retname = newFileName;
                    var filePath = webRootPath + "/upload/" + newFileName;
                    using (var stream = new FileStream(filePath, FileMode.Create))
                    {
                        formFile.CopyTo(stream);
                    }
                }
            }
            return retname;
        }
        [HttpGet]
        public IActionResult DownLoad(string file)
        {
            var addrUrl = "wwwroot/upload/" + file;
            var stream = System.IO.File.OpenRead(addrUrl);
            var shuzu = file.Split('.');
            string fileExt = "";
            foreach (var item in shuzu)
            {
                fileExt = "." + item;
            }
            //获取文件的ContentType
            var provider = new FileExtensionContentTypeProvider();
            var memi = provider.Mappings[fileExt];
            return File(stream, memi, Path.GetFileName(addrUrl));
        }
        #endregion
    }
    public class RetClass
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public string PeopleName { get; set; }
        public int DingDanShu { get; set; }
        public int PaiMing { get; set; }
        public int JinE { get; set; }
        public string JinEBig { get; set; }
    }
}
